package mma;

import robocode.HitRobotEvent;
import robocode.Robot;
import robocode.RobotDeathEvent;
import robocode.ScannedRobotEvent;
/**
 * 
 * @author Matthew Mizumoto
 * 
 * Robocode robot that fires when it sees an enemy.
 *
 */

public class MizuOnegai extends Robot {
  String trackName; //the name of the robot we are tracking
  
  /**
   * method required for all robots to run code.
   */
  public void run() {
    trackName = null; // Initialize to not tracking anyone
    
    while (true)  {
      turnRadarRight(90);  //causes robot to always be scanning
    }    
    
  }
  
  /**
   * Event handler for when a robot is scanned.
   * @param e contains information about the scanned robot.
   */
  public void onScannedRobot(ScannedRobotEvent e) {
    if (trackName != null && !e.getName().equals(trackName)) {  //already has target
      return;
    }
    if (trackName == null) {
      trackName = e.getName();  //sets the new target to follow
    }
    /*
     * moves the robot toward the robot it selected to follow
     */
    if (e.getDistance() > 50) {
      turnRight(e.getBearing());
      ahead(e.getDistance() - 40);
    }
    fire(3);
  }
  
  /**
   * sets the robot to track to null
   * allowing the robot to find a new target.
   * For use of a battle containing more than 2 robots.
   * @param e contains information about dead robot.
   * 
   */
  public void onRobotDeath(RobotDeathEvent e)  {
    trackName = null;
  }
  
  /**
   * has the robot move back if it collides with another robot.
   * @param e contains information about robot it hit
   */
  public void onHitRobot(HitRobotEvent e)  {
    fire(3);
    back(50);
  }

}
